package com.jyu.sati.business.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jyu.sati.business.dao.DocumentDao;
import com.jyu.sati.business.service.HomeService;
import com.jyu.sati.common.exception.BusinessException;
import com.jyu.sati.common.util.UploadUtil;
import com.jyu.sati.entity.Document;

/**
 * 首页服务
 * 
 * @author 淋雨又调皮
 *
 */
@Service
public class HomeServiceImpl extends BaseServiceImpl implements HomeService {
	@Autowired
	private DocumentDao documentDao;

	@Transactional
	@Override
	public List<Document> getAllHomeImages() throws BusinessException {
		List<Document> images = null;
		try {
			images = documentDao.getAllHomeImages(Document.DUCUMENT_TYPE_HOME_PAGE_IMAGE);
		} catch (Exception e) {
			errorLog("获取首页宣传图片失败！", e);
			throw new BusinessException(getMsg());
		}
		return images;
	}

	@Transactional
	@Override
	public void deleteHomeImage(Integer imageId) throws BusinessException {
		if (imageId == null) {
			errorLog("传入参数为空！");
			throw new BusinessException(getMsg());
		}
		try {
			Document image = null;
			try {
				image = documentDao.selectByPrimaryKey(imageId);
			} catch (Exception e) {
				errorLog("获取首页图片数据失败！", e);
				throw new BusinessException(getMsg());
			}
			try {
				if (documentDao.deleteByPrimaryKey(image.getDocumentId()) < 1) {
					throw new BusinessException("没有数据被删除!");
				}
			} catch (Exception e) {
				errorLog("删除数据库数据失败！", e);
				throw new BusinessException(getMsg());
			}
			try {
				UploadUtil.deleteFile(image.getDocumentUrl());
			} catch (Exception e) {
				errorLog("删除首页图片文件失败！", e);
				throw new BusinessException(getMsg());
			}
		} catch (Exception e) {
			errorLog("删除首页宣传图片失败！", e);
			throw new BusinessException(getMsg());
		}
	}

}
